<template>
  <div>
    <template>
      <!-- 一条文章单元格 -->
      <van-cell @click="$router.push('/articlesDetail/' + artObj.art_id)">
        <!-- 标题区域的插槽 -->
        <template #title>
          <div class="title-box">
            <!-- 标题 -->
            <span>{{ artObj.title }}</span>
            <!-- 单图 -->
            <van-image
              v-if="artObj.cover.type === 1"
              class="thumb"
              :src="artObj.cover.images[0]"
              lazy-load
            >
              <template v-slot:error>图片搬家了</template>
            </van-image>
          </div>
        </template>
        <!-- label 区域的插槽 -->
        <template #label>
          <!-- 三张图片 -->
          <div class="thumb-box" v-if="artObj.cover.type > 1">
            <van-image
              class="thumb"
              v-for="(imgUrl, index) in artObj.cover.images"
              :src="imgUrl"
              :key="index"
              lazy-load
            >
              <template v-slot:error>图片搬家了</template>
            </van-image>
          </div>
          <div class="label-box">
            <!-- 文章信息 -->
            <div>
              <span>{{ artObj.aut_name }}</span>
              <span>{{ artObj.comm_count }} 评论</span>
              <span>{{ formatTime(artObj.pubdate) }}</span>
            </div>
            <!-- 反馈按钮 -->
            <van-icon v-if="isShow" name="cross" @click.stop="show = true" />
          </div>
        </template>
      </van-cell>
      <!-- 动作面板 -->
      <van-action-sheet
        v-model="show"
        :cancel-text="cancelText"
        :actions="actions"
        @select="onSelect"
        @cancel="onCancel"
        @closed="onClosed"
      />
    </template>
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import { firstActions, secondActions } from '@/api/reports'
export default {
  props: {
    artObj: Object, // 文章对象
    isShow: {
      type: Boolean,
      default: true
    } // 控制反馈按钮
  },
  methods: {
    formatTime: timeAgo,
    // 面板的点击事件
    onSelect (action) {
      if (action.name === '反馈垃圾内容') {
        // 证明点击的是反馈垃圾内容
        this.actions = this.secondActions
        this.cancelText = '返回' // 修改底部按钮显示文字
      } else if (action.name === '不感兴趣') {
        this.$emit('dislike', this.artObj)
        this.show = false // 关闭弹窗
      } else {
        // 这里就是二级反馈
        this.$emit('reports', this.artObj.art_id, action.value)
        this.show = false // 关闭弹窗
        this.actions = firstActions
        this.cancelText = '取消'
      }
    },
    // 面板中的取消点击事件
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    },
    // 面板关闭事件
    onClosed () {
      this.actions = firstActions
      this.cancelText = '取消'
    }
  },
  data () {
    return {
      show: false, // 控制面板显示/隐藏
      actions: firstActions, // 面板一级数据
      secondActions: secondActions, // 面板二级数据
      cancelText: '取消' // 面板 取消/返回 数据
    }
  }
}
</script>

<style lang="less" scoped>
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  margin-bottom: 6px;
  justify-content: space-between;
}
</style>
